import axios from 'axios';
// import Global from '@/Global'
// import mUtils from '@/utils/mUtils'

// let base = 'https://api-dev.guojutech.net';
// let defaultAppPath = "/factoring-boss";
// let base = 'http://localhost:8010' 
// let base = 'http://1.14.97.197:8010'
let base = 'http://www.luoyunhai.top'

let defaultAppPath = "";

// if (process.env.NODE_ENV != 'development') {
//     let env = require('../config/env');
//     base = env.API_URL;
//     defaultAppPath = "/factoring-boss";
// }

axios.defaults.withCredentials = true;//让ajax携带cookie
// axios.interceptors.request.use(function (config) {
//     return config;
// }, function (error) {
//     if (error && error.response && error.response.status == 401) {
//         mUtils.removeStore(Global.loginUserKey);
//         window.location.href="/";
//     }
// });

const buildUrl = function (url, appPath) {
    if (!url) {
        url = "";
    }
    if (url && url.length > 0 && !url.startsWith("/")) {
        url = "/" + url;
    }
    if (!appPath) {
        appPath = defaultAppPath;
    }
    if (appPath && appPath.length > 0 && !appPath.startsWith("/")) {
        appPath = "/" + appPath;
    }
    return `${base}` + appPath + url;
};

const postApi = function (url, appPath) {
    return params => {
        let config = params ? {params: params} : {};
        return axios.post(buildUrl(url, appPath), null, config)
            .then(res => res.data);
    }
};

const postJsonApi = function (url, appPath) {
    return json => {
        let config = {};
        let data = json ? json : null;
        return axios.post(buildUrl(url, appPath), data, config)
            .then(res => res.data);
    }
};

const getApi = function (url, appPath) {
    return params => {
        let config = params ? {params: params} : {};
        return axios.get(buildUrl(url, appPath), config)
            .then(res => res.data);
    };
};

export {
    buildUrl,
    postApi,
    postJsonApi,
    getApi
}
